<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./jquery.min.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        .wrapper {
            width: 1200px;
            margin: 0 auto;
        }

        .box1 {
            height: 50px;
            background-color: gray;
        }

        .box2 {
            height: 140px;
            background-color: pink;
        }

        .box3 {
            height: 70px;
            background-color: orange;
        }

        .box4 {
            height: 2000px;
            background-color: burlywood;
            
        }
    </style>
</head>

<body>
    <div class="wrapper box1">1</div>
    <div class="wrapper box2">2</div>
    <div class="wrapper box3">3</div>
    <div class="wrapper box4">4</div>
    <script>
        var box3Top = $('.box3').offset().top
        var box3Left = $('.box3').offset().left
        var box4Top = $('.box4').offset().top
        var box4Left = $('.box4').offset().left
        $(window).scroll(function () {
            console.log(1);

            if ($('body, html').scrollTop() >= box3Top) {
                $('.box3').css({
                    position: 'fixed',
                    top: 0,
                    left: box3Left,
                    zIndex: 3
                })
                $('.box4').css({
                    position: 'absolute',
                    top: $('.box4').offset().top,
                    left: $('.box4').offset().left
                })
            } else {
                $('.box3').css({
                    position: 'absolute',
                    top: box3Top,
                    left: box3Left
                })
                $('.box4').css({
                    position: 'absolute',
                    top: box4Top,
                    left: box4Left,
                    // marginTop: $('.box3').height()
                })
            }
        })
    </script>
</body>

</html>